const formatTime = date => {
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const hour = date.getHours();
	const minute = date.getMinutes();
	const second = date.getSeconds();
	return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(
		':');
};


const formatNumber = n => {
	n = n.toString();
	return n[1] ? n : '0' + n;
};

const formatHtml = content => {
	content = content.replace(/\<img/gi,
		'<img style="width:100% !important;height:auto !important;margin:0;display:flex;" ');
	content = content.replace(/\<td/gi,
		'<td  cellspacing="0" cellpadding="0" border="0" style="display:block;vertical-align:top;margin: 0px; padding: 0px; border: 0px;outline-width:0px;" '
	);
	content = content.replace(/width=/gi, 'sss=');
	content = content.replace(/height=/gi, 'sss=');
	content = content.replace(/ \/\>/gi,
		' style="max-width:100% !important;height:auto !important;margin:0;display:block;" \/\>');
	return content;
};

/**
 * 获取链接上的参数
 */
const getUrlKey = (name) => {
	return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || ['',
			''
		])[1]
		.replace(/\+/g, '%20')) || null
}

/**
 * 移除购物车Tabbar的数字
 */
const removeTabBadge = () => {
	let pl = ''
	// #ifdef MP-WEIXIN
	pl = 'mp'
	// #endif
	uni.removeTabBarBadge({
		index: pl == 'mp' ? 2 : 2
	})
}

/**
 * 移除购物车Tabbar的数字
 */
const unOpen = (title) => {
	uni.showToast({
		icon: "none",
		title: title || '该功能未开源'
	});
}
const f_Phone = (data) => {
	if (data) {
		return data.replace(/^(.{3}).+(.{4})$/, '$1****$2');
	}
}
const _debounce = (func, delay) => {
	let timer = null;
	  return function (...args) {
	    if (timer) clearTimeout(timer);
	    timer = setTimeout(() => {
	      func.apply(this, args);
	    }, delay)
	  }
}

const _throttle = (fn, interval) => {
    var enterTime = 0;//触发的时间
    var gapTime = interval || 300 ;//间隔时间，如果interval不传，则默认300ms
    return function(...args) {
      var context = this;
      var backTime = new Date();//第一次函数return即触发的时间
      if (backTime - enterTime > gapTime) {
        fn.apply(this,args);
        enterTime = backTime;//赋值给第一次触发的时间，这样就保存了第二次触发的时间
      }
    };
  }

  // 两点间距离lat1,  lng1,  lat2,  lng2
const  getMapDistance = (paramObj)=> {
      // console.log(paramObj)
      var lng1 = paramObj.lng1
      var lat1 = paramObj.lat1
  
      var lng2 = Number(paramObj.lng2)
      var lat2 = Number(paramObj.lat2)
      
      var radLat1 = lat1*Math.PI / 180.0;
      var radLat2 = lat2*Math.PI / 180.0;
      var a = radLat1 - radLat2;
      var  b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
      var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
      Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
      s = s *6378.137 ;// EARTH_RADIUS;
      s = Math.round(s * 10000) / 10000;
      
      s = s * 1000
      
      if (isNaN(s)) {  
          return 0+'m';  
      }  
      
      if (s > 1000) {
          //    大于1000米时
          s = Math.floor(s/1000 * 100) / 100;
          s = s + 'km'
      } else {
          //    小于1000米直接返回
          s = s + 'm'
      }
      
      return Number(s).toFixed(2);
  }
module.exports = {
	formatTime: formatTime,
	getUrlKey: getUrlKey,
	formatHtml: formatHtml,
	removeTabBadge: removeTabBadge,
	unOpen,
	f_Phone,
	_debounce: _debounce,
	_throttle: _throttle,
	getMapDistance
};